package com.own.dondeestoy;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.own.dondeestoy.core.Parameter;
import com.own.dondeestoy.logic.LayerLogic;

public class SettingsActivity extends Activity {
	// Privadas
	private static LayerLogic _logic;
	private static List<Parameter> params;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		final ActionBar actionBar = getActionBar();
		actionBar.hide();
		_logic = new LayerLogic(MainActivity.context);

		try {
			params = _logic.getAllParameter();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			params = new ArrayList<Parameter>();
			params.add(new Parameter());
		}

		// Selecciono el radio de metros que viene de la base
		int idDist = Integer.valueOf(params.get(1).getValue());
		switch (idDist) {
		case 100:
			RadioButton rd0 = (RadioButton) findViewById(R.id.rdb100me);
			rd0.setChecked(true);
			break;
		case 500:
			RadioButton rd1 = (RadioButton) findViewById(R.id.rdb500me);
			rd1.setChecked(true);
			break;
		case 1000:
			RadioButton rd2 = (RadioButton) findViewById(R.id.rdb1000me);
			rd2.setChecked(true);
			break;
		}

		// Selecciono el radio de metros que viene de la base
		int idMinutes = Integer.valueOf(params.get(0).getValue());
		switch (idMinutes) {
		case 5:
			RadioButton rd0 = (RadioButton) findViewById(R.id.rdb5mi);
			rd0.setChecked(true);
			break;
		case 10:
			RadioButton rd1 = (RadioButton) findViewById(R.id.rdb10mi);
			rd1.setChecked(true);
			break;
		case 15:
			RadioButton rd2 = (RadioButton) findViewById(R.id.rdb15mi);
			rd2.setChecked(true);
			break;
		}

		// Radio group de distancia
		RadioGroup rGroupDist = (RadioGroup) findViewById(R.id.rdgDist);
		rGroupDist
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					public void onCheckedChanged(RadioGroup rGroup,
							int checkedId) {
						// This will get the radiobutton that has changed in its
						// check state
						RadioButton checkedRadioButton = (RadioButton) rGroup
								.findViewById(checkedId);

						// Actualizo la tabla de paramétros con el id
						String dist = checkedRadioButton.getTag().toString();
						
						Parameter param = null;
						try {
							param = _logic.getParameterById(2);
							param.setValue(dist);
							_logic.updateParameter(param);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});

		// Radio group de tiempo
		RadioGroup rGroupTime = (RadioGroup) findViewById(R.id.rdgTime);
		rGroupTime
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					public void onCheckedChanged(RadioGroup rGroup,
							int checkedId) {
						// Actualizo la tabla de paramétros con el id
						RadioButton checkedRadioButton = (RadioButton) rGroup
								.findViewById(checkedId);

						// Actualizo la tabla de paramétros con el id
						String time = checkedRadioButton.getTag().toString();

						Parameter param = null;
						try {
							param = _logic.getParameterById(1);
							param.setValue(time);
							_logic.updateParameter(param);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

		CheckBox chkHistory = (CheckBox) findViewById(R.id.chkHistory);// Bicis
		chkHistory.setEnabled(true);
		CheckBox chkTango = (CheckBox) findViewById(R.id.chkTango);//Subtes
		chkTango.setEnabled(true);
		CheckBox chkHospitals = (CheckBox) findViewById(R.id.chkHospitals);
		chkHospitals.setEnabled(false);
		CheckBox chkbar = (CheckBox) findViewById(R.id.chkbar);
		chkbar.setEnabled(true);
		CheckBox chkSummer = (CheckBox) findViewById(R.id.chkSummer);// Bibliotecas
		chkSummer.setEnabled(true);
		CheckBox chkHostels = (CheckBox) findViewById(R.id.chkHostels);
		chkHostels.setEnabled(true);
		CheckBox chkCultural = (CheckBox) findViewById(R.id.chkCultural);
		chkCultural.setEnabled(false);
		CheckBox chkWifi = (CheckBox) findViewById(R.id.chkWifi);
		chkWifi.setEnabled(true);
		
		
		String parameters = params.get(2).getValue().replace(".", "");
		int isSelected = 0;
		for (int i = 0; i <= parameters.length() - 1; i++) {
			isSelected = Integer.valueOf(parameters.substring(i, i + 1));
			switch (isSelected) {
			case 1:
				chkHistory.setChecked(true);
				break;
			case 2:
				chkTango.setChecked(true);
				break;
			case 3:
				chkHospitals.setChecked(true);
				break;
			case 4:
				chkbar.setChecked(true);
				break;
			case 5:
				chkSummer.setChecked(true);
				break;
			case 6:
				chkHostels.setChecked(true);
				break;
			case 7:
				chkCultural.setChecked(true);
				break;
			case 8:
				chkWifi.setChecked(true);
				break;
			}
		}

		chkHistory.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 1);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("1", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		chkTango.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 2);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("2", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		chkHospitals.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 3);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("3", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		chkbar.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 4);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("4", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		chkSummer.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 5);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("5", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		chkHostels.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 6);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("6", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		chkCultural.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 7);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("7", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		chkWifi.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Parameter param = null;
				try {
					param = _logic.getParameterById(3);
					if (isChecked) {
						String value = param.getValue();
						param.setValue(value + 8);
						_logic.updateParameter(param);
					} else {
						String value = param.getValue();
						param.setValue(value.replace("8", ""));
						_logic.updateParameter(param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

	}
	
	@Override
	public void onBackPressed() {
		stopService(new Intent(getBaseContext(), CheckLocationService.class));
	    startService(new Intent(getBaseContext(), CheckLocationService.class));
	    super.onBackPressed();
	    return;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

}
